package com.it.travel01.controller;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class VerificationController {


    @Value("${constant.checkCodekey}")
    private String checkCodekey;

    @RequestMapping("checkCode")
    public void getVerification(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.设置浏览器禁止缓存当前响应的数据
        response.setHeader("pragma", "no-cache");
        response.setHeader("cache-control", "no-cache");
        response.setHeader("expires", "0");

        int weight = 80;
        int height = 30;
        BufferedImage image = new BufferedImage(weight,height,BufferedImage.TYPE_INT_RGB);

        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.BLACK);
        graphics.fillRect(0,0,weight,height);
        graphics.drawRect(0,0,weight-1,height-1);

        graphics.setColor(Color.ORANGE);
        graphics.setFont(new Font("黑体",Font.BOLD,24));
        String randomStr = RandomStringUtils.randomAlphabetic(1);
        request.getSession().setAttribute(checkCodekey,randomStr);
        graphics.drawString(randomStr,15,25);


        ImageIO.write(image, "jpg", response.getOutputStream());


    }
}
